package com.walming.generator.copycat.extend;

import com.walming.generator.copycat.constant.PageConstants;
import com.walming.generator.copycat.utils.CriteriaUtils;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 基本查询参数
 * </p>
 *
 * @author walming
 * @date 2021-03-09 14:03
 */
public class BaseQueryParam {

    /** 排序列（多个用逗号分隔） */
    @ApiModelProperty(value = "排序列（多个用逗号分隔）", example = "createTime", position = 101)
    private String orderByColumn;
    /** 排序方向（asc正序、desc倒序），默认倒序 */
    @ApiModelProperty(value = "排序方向（asc正序、desc倒序），默认倒序", example = "desc", position = 102)
    private String isAsc;

    /** 当前记录起始索引 */
    @ApiModelProperty(value = "当前记录起始索引", example = "1", position = 103)
    private Integer pageNum;
    /** 每页显示记录数(0表示查询出全部) */
    @ApiModelProperty(value = "每页显示记录数(0表示查询出全部)", example = "10", position = 104)
    private Integer pageSize;

    public BaseQueryParam() {
    }

    public BaseQueryParam(Integer pageNum, Integer pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public String getOrderByColumn() {
        return this.orderByColumn;
    }

    public void setOrderByColumn(String orderByColumn) {
        this.orderByColumn = orderByColumn;
    }

    public String getIsAsc() {
        return ("asc".equals(this.isAsc) || "desc".equals(this.isAsc)) ? this.isAsc : PageConstants.ASC;
    }

    public void setIsAsc(String isAsc) {
        this.isAsc = isAsc;
    }

    public Integer getPageNum() {
        return this.pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 快捷配置升序排列
     *
     * @param columns 字段数组
     */
    public void orderByAsc(String... columns) {
        this.orderBy(true, columns);
    }

    /**
     * 快捷配置降序排列
     *
     * @param columns 字段数组
     */
    public void orderByDesc(String... columns) {
        this.orderBy(false, columns);
    }

    /**
     * 指定需要排序的列，根据条件确认是否进行升序排列，否则进行降序排列
     *
     * @param isAsc   是否是 ASC 排序
     * @param columns 字段数组
     */
    public void orderBy(boolean isAsc, String... columns) {
        this.isAsc = isAsc ? PageConstants.ASC : PageConstants.DESC;
        this.orderByColumn = StringUtils.join(columns, ",");
    }

    /**
     * 获取排序拼接字符串, 默认驼峰转下划线
     */
    public String getOrderBy() {
        return this.getOrderBy(true);
    }

    /**
     * 获取排序拼接字符串
     *
     * @param isCamel 是否启用驼峰转下划线
     */
    public String getOrderBy(boolean isCamel) {
        String columns = isCamel ? CriteriaUtils.toUnderscore(this.orderByColumn) : this.orderByColumn;
        return StringUtils.isEmpty(columns) ? "" : columns + " " + this.getIsAsc();
    }

}
